export default {
  namespaced: true,
  state: {
    cartList: []
  },
  getters: {
    Allnum (state) {
      let number = 0
      state.cartList.forEach(item => (number += item.num))
      return number
    },
    Allprice (state) {
      let price = 0
      state.cartList.forEach(item => {
        if (item.checked) {
          price += item.num * item.price
        }
      })
      return price * 100
    }
  },
  mutations: {
    addToCart (state, goods) {
      const temgoods = state.cartList.find(item => item.id === goods.id)
      if (temgoods) {
        temgoods.num++
      } else {
        state.cartList.push(goods)
      }
    },
    //  根据id删商品
    delGoods (state, id) {
      const index = state.cartList.findIndex(item => item.id === id)
      state.cartList.splice(index, 1)
    }
  }
}
